#include <cstdlib>
#include <ctime>
#include <sys/types.h>
#include <unistd.h>

#include "RingQueue.hpp"

void* consumer(void* args)
{
    RingQueue* rq = (RingQueue*)args;
    int data;
    while(1)
    {
        rq->pop(data);
        std::cout << "Consume: " << data << " [" << pthread_self() << "]" << std::endl;
    }
}

void* productor(void* args)
{
    RingQueue* rq=(RingQueue*)args;
    while(1)
    {
        int data=rand()%100 + 1;
        std::cout << "Product: " << data << " [" << pthread_self() << "]" << std::endl;
        rq->push(data);
        sleep(1);
    }
}

int main()
{
    srand((uint64_t)time(nullptr)^getpid());
    RingQueue* rqueue = new RingQueue();

    pthread_t c[3];
    pthread_create(c, nullptr, consumer, rqueue);
    pthread_create(c+1, nullptr, consumer, rqueue);
    pthread_create(c+2, nullptr, consumer, rqueue);
    pthread_t p[2];
    pthread_create(p, nullptr, productor, rqueue);
    pthread_create(p+1, nullptr, productor, rqueue);

    for(int i=0; i<3; i++)
        pthread_join(c[i], nullptr);
    for(int i=0; i<2; i++)
        pthread_join(p[i], nullptr);
    return 0;
}